home *** CD-ROM | disk | FTP | other *** search
/ SuperHack / SuperHack CD.bin / CODING / CPP / WFC010.ZIP / SRC / CUNC.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1995-12-07  |  6.5 KB  |  313 lines

  1. #include <wfc.h>
  2. #pragma hdrstop
  3.  
  4. /*
  5. ** Author: Samuel R. Blackburn
  6. ** CI$: 76300,326
  7. ** Internet: sammy@sed.csc.com
  8. **
  9. ** You can use it any way you like as long as you don't try to sell it.
  10. **
  11. ** Any attempt to sell WFC in source code form must have the permission
  12. ** of the original author. You can produce commercial executables with
  13. ** WFC but you can't sell WFC.
  14. **
  15. ** Copyright, 1995, Samuel R. Blackburn
  16. **
  17. ** $Workfile: $
  18. ** $Revision: $
  19. ** $Modtime: $
  20. */
  21.  
  22. #if defined( _DEBUG )
  23. #undef THIS_FILE
  24. static char BASED_CODE THIS_FILE[] = __FILE__;
  25. #endif
  26.  
  27. IMPLEMENT_SERIAL( CUniversalNamingConvention, CObject, 1 );
  28.  
  29. #if defined( _DEBUG )
  30. #define new DEBUG_NEW
  31. #endif
  32.  
  33. CUniversalNamingConvention::CUniversalNamingConvention()
  34. {
  35.    Empty();
  36. }
  37.  
  38. CUniversalNamingConvention::CUniversalNamingConvention( const CUniversalNamingConvention& source )
  39. {
  40.    Copy( source );
  41. }
  42.  
  43. CUniversalNamingConvention::CUniversalNamingConvention( const CUniformResourceLocator& source )
  44. {
  45.    Copy( source );
  46. }
  47.  
  48. CUniversalNamingConvention::CUniversalNamingConvention( LPCTSTR source )
  49. {
  50.    Copy( source );
  51. }
  52.  
  53. CUniversalNamingConvention::~CUniversalNamingConvention()
  54. {
  55.    Empty();
  56. }
  57.  
  58. int CUniversalNamingConvention::Compare( const CUniversalNamingConvention& source )
  59. {
  60.    ASSERT_VALID( this );
  61.  
  62.    if ( this == &source )
  63.    {
  64.       return( 0 );
  65.    }
  66.  
  67.    return( UNC.CompareNoCase( source.UNC ) );
  68. }
  69.  
  70. void CUniversalNamingConvention::Copy( const CUniversalNamingConvention& source )
  71. {
  72.    ASSERT_VALID( this );
  73.    ASSERT( this != &source );
  74.  
  75.    /*
  76.    ** Make sure we ain't copying ourselves
  77.    */
  78.  
  79.    if ( this == &source )
  80.    {
  81.       return;
  82.    }
  83.  
  84.    ServerName = source.ServerName;
  85.    ShareName  = source.ShareName;
  86.    PathName   = source.PathName;
  87.  
  88.    /*
  89.    ** Its safer is we construct the UNC
  90.    */
  91.  
  92.    Make();
  93. }
  94.  
  95. void CUniversalNamingConvention::Copy( const CUniformResourceLocator& source )
  96. {
  97.    ASSERT_VALID( this );
  98.  
  99.    Empty();
  100.  
  101.    ServerName = source.MachineName;
  102.    PathName   = source.PathName;
  103.  
  104.    int location_of_slash = PathName.Find( '/' );
  105.  
  106.    if ( location_of_slash != (-1) )
  107.    {
  108.       ShareName = PathName.Left( location_of_slash );
  109.       PathName  = PathName.Right( ( PathName.GetLength() - location_of_slash ) - 1 );
  110.    }
  111.  
  112.    /*
  113.    ** Now go through the Path and convert /'s to \'s
  114.    */
  115.  
  116.    location_of_slash = 0;
  117.  
  118.    while( location_of_slash < PathName.GetLength() )
  119.    {
  120.       if ( PathName[ location_of_slash ] == '/' )
  121.       {
  122.          PathName.SetAt( location_of_slash, '\\' );
  123.       }
  124.  
  125.       location_of_slash++;
  126.    }
  127.  
  128.    Make();
  129. }
  130.  
  131. void CUniversalNamingConvention::Copy( LPCTSTR source )
  132. {
  133.    ASSERT_VALID( this );
  134.    ASSERT( source != NULL );
  135.  
  136.    TRACE( "CUniversalNamingConvention::Copy( \"%s\" );\n", source );
  137.  
  138.    /*
  139.    ** Input should be "\\server_name\share_name\directory\filename.ext"
  140.    */
  141.  
  142.    /*
  143.    ** Make sure we start out with a virgin object
  144.    */
  145.  
  146.    Empty();
  147.  
  148.    if ( source == NULL )
  149.    {
  150.       return;
  151.    }
  152.  
  153.    CString temp_string( source );
  154.  
  155.    if ( temp_string.GetLength() < 2 || temp_string.Left( 2 ) != "\\\\" )
  156.    {
  157.       PathName = temp_string;
  158.       Make();
  159.       return;
  160.    }
  161.  
  162.    /*
  163.    ** See if the first two characters are back slashes, if so, rip them off
  164.    */
  165.  
  166.    while( temp_string[ 0 ] == '\\' )
  167.    {
  168.       temp_string = temp_string.Right( temp_string.GetLength() - 1 );
  169.    }
  170.  
  171.    int location_of_back_slash = temp_string.Find( '\\' );
  172.  
  173.    /*
  174.    ** First field is ServerName
  175.    */
  176.  
  177.    if ( location_of_back_slash == (-1) )
  178.    {
  179.       /*
  180.       ** The user sent us something like "\\dodah.txt"
  181.       */
  182.  
  183.       PathName = temp_string;
  184.       Make();
  185.       return;
  186.    }
  187.  
  188.    /*
  189.    ** First field is server name
  190.    */
  191.  
  192.    ServerName = temp_string.Left( location_of_back_slash );
  193.  
  194.    temp_string = temp_string.Right( ( temp_string.GetLength() - location_of_back_slash ) - 1 );
  195.  
  196.    /*
  197.    ** Second field is ShareName
  198.    */
  199.  
  200.    location_of_back_slash = temp_string.Find( '\\' );
  201.  
  202.    if ( location_of_back_slash == (-1) )
  203.    {
  204.       /*
  205.       ** User sent us something like "\\server_name\"
  206.       */
  207.  
  208.       Make();
  209.       return;
  210.    }
  211.  
  212.    ShareName = temp_string.Left( location_of_back_slash );
  213.    PathName  = temp_string.Right( ( temp_string.GetLength() - location_of_back_slash ) - 1 );
  214.  
  215.    Make();
  216. }
  217.  
  218. void CUniversalNamingConvention::Empty( void )
  219. {
  220.    ASSERT_VALID( this );
  221.  
  222.    ServerName.Empty();
  223.    ShareName.Empty();
  224.    PathName.Empty();
  225.    UNC.Empty();
  226. }
  227.  
  228. void CUniversalNamingConvention::Make( void )
  229. {
  230.    ASSERT_VALID( this );
  231.  
  232.    TRACE( "CUniversalNamingConvention::Make() : Adding \"\\\\\" + \"%s\" + \"\\\" + \"%s\" + \"\\\" + \"%s\"\n",
  233.           (LPCTSTR) ServerName,
  234.           (LPCTSTR) ShareName,
  235.           (LPCTSTR) PathName );
  236.  
  237.    UNC  = "\\\\";
  238.    UNC += ServerName;
  239.    UNC += "\\";
  240.    UNC += ShareName;
  241.    UNC += "\\";
  242.    UNC += PathName;
  243. }
  244.  
  245. void CUniversalNamingConvention::Serialize( CArchive& archive )
  246. {
  247.    CObject::Serialize( archive );
  248.  
  249.    if ( archive.IsStoring() )
  250.    {
  251.       archive << ServerName;
  252.       archive << ShareName;
  253.       archive << PathName;
  254.       archive << UNC;
  255.    }
  256.    else
  257.    {
  258.       archive >> ServerName;
  259.       archive >> ShareName;
  260.       archive >> PathName;
  261.       archive >> UNC;
  262.    }
  263. }
  264.  
  265. CUniversalNamingConvention& CUniversalNamingConvention::operator = ( const CUniversalNamingConvention& source )
  266. {
  267.    ASSERT_VALID( this );
  268.    Copy( source );
  269.    return( *this );
  270. }
  271.  
  272. CUniversalNamingConvention& CUniversalNamingConvention::operator = ( const CUniformResourceLocator& source )
  273. {
  274.    ASSERT_VALID( this );
  275.    Copy( source );
  276.    return( *this );
  277. }
  278.  
  279. CUniversalNamingConvention& CUniversalNamingConvention::operator = ( LPCTSTR source )
  280. {
  281.    ASSERT_VALID( this );
  282.    Copy( source );
  283.    return( *this );
  284. }
  285.  
  286. BOOL CUniversalNamingConvention::operator == ( const CUniversalNamingConvention& right_unc )
  287. {
  288.    ASSERT_VALID( this );
  289.  
  290.    if ( Compare( right_unc ) == 0 )
  291.    {
  292.       return( TRUE );
  293.    }
  294.    else
  295.    {
  296.       return( FALSE );
  297.    }
  298. }
  299.  
  300. #if defined( _DEBUG )
  301.  
  302. void CUniversalNamingConvention::Dump( CDumpContext& dump_context ) const
  303. {
  304.    CObject::Dump( dump_context );
  305.  
  306.    dump_context << "ServerName = \"" << ServerName << "\"\n";
  307.    dump_context << "ShareName = \"" << ShareName << "\"\n";
  308.    dump_context << "PathName = \"" << PathName << "\"\n";
  309.    dump_context << "UNC = \"" << UNC << "\"\n";
  310. }
  311.  
  312. #endif // _DEBUG
  313.